<template>
  <div class="selector-wrap">
    <div>
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input v-model="queryParams.keywords" placeholder="关键字" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="所属分类" prop="categoryId">
          <el-tree-select v-model="queryParams.categoryIds" placeholder="请选择所属分类" :data="categoryOptions" filterable
            check-strictly :render-after-expand="false" />
        </el-form-item>
        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableDatas" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="定额项" prop="name" />
      <el-table-column label="定额分类" width="150" align="center" prop="categoryName" />
      <el-table-column label="价格" width="120" align="center" prop="price" />
      <el-table-column label="成本" width="120" align="center" prop="costPrice" />
      <el-table-column label="单位" align="center" prop="unit" width="120" />
      <el-table-column label="描述" align="center" prop="remark" width="160" />
    </el-table>

    <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from "vue";
import NormAPI, { NormPageQuery, NormPageVO } from "@/api/business/material/norm.api";
import NormCategoryAPI from "@/api/business/material/normCategory.api";

const props = defineProps({
  modelValue: {
    type: Array,
    default: () => [],
  },
});

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
}) as NormPageQuery;

const categoryOptions = ref<OptionType[]>();

const emit = defineEmits(["update:modelValue", "selection-change"]);

const checked = ref(Array<any>());
const handleSelectionChange = (selection: Array<any>) => {
  checked.value = selection;
  emit("update:modelValue", selection);
  emit("selection-change", selection);
};

function getChecked(): NormPageVO[] {
  return checked.value || [];
}

const total = ref(0);
const tableDatas = ref<NormPageVO[]>();

const handleQuery = () => {
  if (queryParams.categoryIds) {
    queryParams.deepSearch = true;
  }
  NormAPI.getPage(queryParams).then((data) => {
    tableDatas.value = data.list;
    total.value = data.total;
    // if (tableDatas.value && tableDatas.value.length > 0) {
    //   tableDatas.value.forEach((element) => {
    //     element.code = element.id;
    //   });
    // }
  });
};

const handleResetQuery = () => {
  queryParams.keywords = "";
  queryParams.pageNum = 1;
  queryParams.categoryIds = "";
  handleQuery();
};

onMounted(() => {
  NormCategoryAPI.getOptions().then((data) => {
    categoryOptions.value = data;
  });
});

defineExpose({
  getChecked,
  handleQuery,
});
</script>

<style scoped>
.selector-wrap {
  padding: 10px;
}
</style>
